package com.hq.fxh.No3_TuiGuang.MyMoBan;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.hq.fxh.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author 大圣
 * @ClassName MasterAdapter
 * @Description : 万能适配器
 * @Created 2015/10/28 0028
 */
public abstract class MasterAdapter<T> extends BaseAdapter {
    private final boolean DEBUG = false;

    private List<T> mDataList;

    private int mViewTypeCount;

    private Object mType;

    private LayoutInflater mInflater;

    private Context mContext;

    private AdapterItemUtil util = new AdapterItemUtil();

    public MasterAdapter(Context context){
        this(context ,new ArrayList<T>());
    }
    public MasterAdapter(Context context , @NonNull List<T> data) {
        this(context,data, 1);
    }
    public MasterAdapter(Context context , T[] data){
        this(context, Arrays.asList(data),1);
    }

    protected MasterAdapter(Context context, @NonNull List<T> data, int viewTypeCount) {
        mContext = context;
        mDataList = data;
        mViewTypeCount = viewTypeCount;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }



    /**
     * 可以被复写用于单条刷新等
     */
    public void updateData(List<T> data) {
        mDataList = data;
        notifyDataSetChanged();
    }

    public void add(T obj){
        if(obj == null)
            return;
        mDataList.add(obj);
        notifyDataSetChanged();
    }
    public void addAll(List<T> data){
        if(data == null || data.isEmpty())
            return;
        mDataList.addAll(data);
        notifyDataSetChanged();
    }

    public void addAll(T[] data){
        if(data == null || data.length == 0)
            return;
        Collections.addAll(mDataList, data);
        notifyDataSetChanged();
    }

    public void clear(){
        mDataList.clear();
        notifyDataSetChanged();
    }
    public void remove(int position){
        mDataList.remove(position);
       notifyDataSetChanged();
    }

    public void remove(T obj){
        int index = mDataList.indexOf(obj);
        mDataList.remove(index);
        notifyDataSetChanged();
    }
    public int getPosition(T item) {
        return mDataList.indexOf(item);
    }


    public T getItem(int position){
        return mDataList.get(position);
    }

    public List<T> getDataList() {
        return mDataList;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * instead by{@link #getItemViewType(Object)}
     */
    @Override
    @Deprecated
    public int getItemViewType(int position) {
        mType = getItemViewType(mDataList.get(position));
        //Log.d("ddd", "getType = " + util.getIntType(mType));
        // 如果不写这个方法，会让listView更换dataList后无法刷新数据
        return util.getIntType(mType);
    }

    public Object getItemViewType(T obj) {
        return obj;
    }

    @Override
    public int getViewTypeCount() {
        return mViewTypeCount;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Log.d("ddd", "getView");
        if (mInflater == null) {
            mInflater = LayoutInflater.from(parent.getContext());
        }

        IAdapterItem<T> item;
        if (convertView == null) {
            item = getItemView(mType);
            convertView = mInflater.inflate(item.getLayoutResId(), parent, false);
            convertView.setTag(R.id.tagItem, item);
            item.onBindViews(convertView);
            item.onSetViews(mContext);
            if (DEBUG) convertView.setBackgroundColor(0xffff0000);
        } else {
            item = (IAdapterItem<T>) convertView.getTag(R.id.tagItem);
            if (DEBUG) convertView.setBackgroundColor(0xff00ff00);
        }

        item.onUpdateViews(mContext , mDataList.get(position), position);
        return convertView;
    }


    public abstract
    @NonNull
    IAdapterItem<T> getItemView(Object type);



}
